{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}
<br>
<style id='style-height'>
    .layui-table td{
        height:50px;
        overflow:visible;
        text-overflow:inherit;
        white-space:normal;
    }
    td .layui-form-select{
            margin-top: -10px;
            margin-left: -15px;
            margin-right: -15px;
    }




</style>
<form class="layui-form" action="" id="cross_srh">
    <div class="proxy_srh">
        <div class="layui-inline">
            <div class="layui-inline">
                <!--一个任务只有一个项目。但多一个项目可能有多个任务-->

                <div class="layui-input-inline" id="mytask">
                    <label class="layui-form-label">我的任务*：</label>
                    <div class="layui-input-inline" style="width:150px">
                        <select class="layui-select" name="do_plan" id="do_plan" lay-verify="" lay-search
                                lay-filter="do_plan">
                            <option value="">请选择</option>
                            {% for d in AllTaskName %}
                            <option value="{{d.project_version}}">{{d.project_version}}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>

                <div class="layui-input-inline" id="taskProject">
                    <label class="layui-form-label">项目*：</label>
                    <div class="layui-input-inline" style="width:150px">
                        <select class="layui-select" name="project_name" id="project_name" lay-verify="" lay-search
                                lay-filter="project_name">
                        </select>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <label class="layui-form-label">级别：</label>
                    <div class="layui-input-inline">
                        <div id="case_pri" style="width: 150px;"></div>
                    </div>
                </div>

                <div class="layui-input-inline">
                    <label class="layui-form-label">结果：</label>
                    <div class="layui-input-inline">
                        <div id="case_result" style="width: 150px;"></div>
                    </div>
                </div>
                <br><br>


                <div class="layui-input-inline" id="case_mod_div">
                    <label class="layui-form-label">主模块：</label>
                    <div class="layui-input-inline">
                        <div id="case_module" style="width: 150px;"></div>
                    </div>
                </div>

                <div class="layui-input-inline" id="case_plan_div">
                    <label class="layui-form-label">用例版本：</label>
                    <div class="layui-input-inline">
                        <div id="case_plan" style="width: 150px;"></div>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <label class="layui-form-label">关键字：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="key_words" id="key_words" placeholder="请输入"
                               style="width:150px">
                    </div>
                </div>
                <div class="layui-input-inline" style="float:right;float:2px">
                    <button class="layui-btn  layui-btn-danger" type="button" id="start" lay-submit
                            lay-filter="start">获取用例
                    </button>
                </div>
            </div>
        </div>

    </div>

</form>
<hr class="layui-bg-gray">
<div class="layui-input-inline" style="">

    <div class="layui-input-inline">
        <input type="text" class="layui-input" name="cellHeigh" id="cellHeigh" value="50"
               style="width:50px;height:30px">
    </div>
    <div class="layui-inline">
        <button class="layui-btn layui-btn-sm" id="setHeight" lay-submit lay-filter="setHeight">列高</button>
    </div>
</div>

<table class="layui-hide" id="doCase" lay-filter="doCase"></table>


<script type="text/html" id="filemange_v1">
    <button type="button" class="layui-btn layui-btn-xs " lay-event="more_edit">高级</button>
    <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="delNode"><i class="layui-icon">
        &#xe640;</i></button>
</script>

<script>
    layui.use(['table','element','xmSelect'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;
          var xmSelect = layui.xmSelect;
          var element = layui.element;
          var resultSelect = xmSelect.render({
              el: '#case_result',
              name:"result",
              data: [{"name":"PASS","value":"PASS"}
                    ,{"name":"FAIL","value":"FAIL"}
                    ,{"name":"BLOCK","value":"BLOCK"}
                    ,{"name":"NT","value":"NT"}
                    //,{"name":"NOT RESULT","value":"NO RESULT"}
              ]
          });
          var priSelect = xmSelect.render({
              el: '#case_pri',
              name:"pri",
              data: [{"name":"高","value":"高"}
                    ,{"name":"中","value":"中"}
                    ,{"name":"低","value":"低"}
              ]
          });

          userId = userId = layui.data('test').UserInfo.userId;

          $("#case_plan_div").hide();
          $("#case_mod_div").hide();

          //表格的加载
          var  options = {
                elem:'#doCase',
                skin:"line",
                page:true,
                height: 'full-200',
                toolbar: true,
                limit:100,
                limits:[100,500,1000],
                response:{
                    statusName:'errorCode',
                    statusCode:'',
                    dataName:'data'
                },
                id:"doCase",
                cols:[[
                       {type: 'numbers',title:'No.'},
                      ,{field: 'case_no', title: '用例编号',hide:true,sort:true}
                      ,{field: 'case_require_no', title: '需求编号',sort:true,hide:true}
                      ,{field: 'case_main_mod', title: '主模块',width:100,sort:true}
                      ,{field: 'case_second_mod',hide:true, title: '子模块',sort:true}
                      ,{field: 'case_pri', title: '级别',sort:true,hide:true}
                      ,{field: 'case_pre', title: '前提',sort:true,event:'t4'}
                      ,{field: 'case_title', title: '*标题*',sort:true,event:'t1'}
                      ,{field: 'case_step', title: '步骤',sort:true,event:'t2'}
                      ,{field: 'case_expect', title: '期待结果',sort:true,event:'t3'}
                      ,{field: 'case_note', title: '备注',hide:true,sort:true}
                      ,{field: 'up_person', title: '用例创建人',sort:true,hide:true,templet: function(d){
                            return d.up_person_name;

                      }}
                      ,{field: 'result', width:110,title: '结果(E)',sort:true,templet: function(d){
                            return '<select name="case_result" lay-filter="case_result" data-value="'+d.result+'">'+
                            '    <option value="">请选择</option>'+
                            '    <option value="PASS">PASS</option>'+
                            '    <option value="FAIL">FAIL</option>'+
                            '    <option value="BLOCK">BLOCK</option>'+
                            '    <option value="NT">NT</option>'+
                            '</select>';

                      }}
                      ,{field: 'bug_url', title: '地址(E)',sort:true,edit:'text'}
                      ,{field: 'bug_des', title: '描述(E)',sort:true,edit:'text'}
                      ,{field: 'update_time', title: '用例时间',sort:true,hide:true}
                      ,{field: 'id', title: '用例Id',sort:true,hide:true}
                      ,{field: 'person', title: '测试人(E)',hide:true,sort:true}
                      ,{field: 'result_id', title: '结果Id',sort:true,hide:true}
                      ,{field: 'imag_desc', title: '图片',sort:true,hide:true}
                      ,{fixed: 'right', title:'', toolbar: '#filemange_v1',width:120}

                     ]]
                ,data:[]
                ,done:function(res,cur,count){

                    var tableElem = this.elem.next('.layui-form.layui-border-box.layui-table-view');
                    count || tableElem.find('.layui-table-header').css('overflow', 'auto');
                    layui.each(tableElem.find('select'), function (index, item) {
                        var elem = $(item);
                        elem.val(elem.data('value')).parents('div.layui-table-cell').css('overflow', 'visible');
                    });

                    layui.each(res.data,function(index,item){
                         if(item.imag_desc==""||item.imag_desc==undefined){
                            }else{
                                $("tr[data-index='"+index+"']>td[data-field='case_title']").css('background','#efccd2');
                            }
                         if(item.result == "PASS"){
                            $("tr[data-index='"+index+"']>td[data-field='result']").css('background','lightgreen');
                         }else if(item.result == "FAIL"){
                            $("tr[data-index='"+index+"']>td[data-field='result']").css('background','orangered');
                         }else if(item.result == "BLOCK"){
                            $("tr[data-index='"+index+"']>td[data-field='result']").css('background','yellow');
                         }
                    });

                    form.render();

                }
          }
          var tableIns = table.render(options);



          table.on('row(doCase)', function(obj){
            obj.tr.css("background","greenyellow");
            if(obj.data.imag_desc==""||obj.data.imag_desc==undefined){
                }else{
                    obj.tr.css("background","#efccd2");
            }

          });

          table.on('edit(doCase)', function(obj){
                rowCache = obj.data;

                data = {"case_version":rowCache.case_plan,"project_id":rowCache.project_id,"case_id":rowCache.id,"result_id":rowCache.result_id,"project_version":$('#do_plan option:selected').val(),"result":rowCache.result,"bug_url":rowCache.bug_url,"bug_des":rowCache.bug_des,"person":userId};
                  //提交到服务端
                $.ajax({
                                    //发出请求
                                    type:"POST",
                                    url:"/basecase/case/result/manage",
                                    contentType: 'application/json; charset=utf-8',
                                    dataType:"json",
                                    data:JSON.stringify(data),
                                    success: function(result){

                                    }
                });

                form.render('');
          });

          table.on('tool(doCase)', function(obj){
                title = "";
                if(obj.event == 't1'){
                    title = "标题";
                    msg = obj.data.case_title;
                }else if(obj.event == 't2'){
                    title = "步骤";
                    msg = obj.data.case_step;
                }else if(obj.event == 't3'){
                    title = "期待结果";
                    msg = obj.data.case_expect;
                }else if(obj.event == 't4'){
                    title = "前提条件";
                    msg = obj.data.case_pre;
                }else{
                    return false;
                }
                if(msg == ""){return false};
                layer.open({
                  title: title+'_详情',
                  area:['572px','273px']
                  ,content: msg
                  ,closeBtn:1,
                  shade: 0,
                  shadeClose:true
                });
          });

          $('#setHeight').on('click',function(){

                height = $('#cellHeigh').val();

                strStyle = '.layui-table td{height:'+height+'px;overflow:visible;text-overflow:inherit;white-space:normal;}';
                $("#style-height").html(strStyle);
          });

          table.on('tool(doCase)', function(obj){
                if(obj.event ==='delNode'){
                    layer.confirm('删除会将用例和结果都删除,真的确认了么?', function(index){
                        resultId = obj.data.result_id;
                        $.ajax({
                                    //发出请求
                                    type:"POST",
                                    url:"/basecase/case/del",
                                    contentType: 'application/json; charset=utf-8',
                                    dataType:"json",
                                    data:JSON.stringify([obj.data.id]),
                                    success: function(result){
                                        //删除用例

                                        if(resultId!=""){
                                            //删除结果
                                            $.ajax({
                                                    //发出请求
                                                    type:"POST",
                                                    url:"/basecase/case/result/delete",
                                                    contentType: 'application/json; charset=utf-8',
                                                    dataType:"json",
                                                    data:JSON.stringify({"id":resultId}),
                                                    success: function(result){

                                                    }
                                            });
                                        }
                                        layer.msg("删除成功!");
                                        obj.del();
                                        layer.close(index);

                                    }
                        });
                      });
                }
                else if (obj.event ==='more_edit'){
                    //进入高级修改的页面
                    if($(".layui-layer-close").length >= 1 ){

                        $(".layui-layer-close").click();
                    }

                    if($(".layui-layer-close").length == 0 ){
                        layui.data('test', {key: 'RowBaseCase',value: obj.data});
                        index=layer.open({
                                                    type:2,
                                                    content:"/basecase/index/manage",
                                                    area:['850px','650px'],
                                                    offset:  'lb',
                                                    title:"编辑用例",
                                                    closeBtn:1,
                                                    shade: 0,
                                                    shadeClose:true,
                                                    maxmin: true,
                                                    cancel: function(index, layero){

                                                    }
                        });
                    }

                }
          });

          //提交测试结果
          form.on('select(case_result)', function(obj){
                  row = obj.elem.parentNode.parentNode.parentNode.rowIndex;
                  table.cache.doCase[row].result = obj.value;

                  rowCache = table.cache.doCase[row];
                  data = {"case_version":rowCache.case_plan,"project_id":rowCache.project_id,"case_id":rowCache.id,"result_id":rowCache.result_id,"project_version":$('#do_plan option:selected').val(),"result":rowCache.result,"bug_url":rowCache.bug_url,"bug_des":rowCache.bug_des,"person":userId};
                  //提交到服务端
                  if(obj.value == "PASS"){
                            $("tr[data-index='"+row+"']>td[data-field='result']").css('background','lightgreen');
                         }else if(obj.value == "FAIL"){
                            $("tr[data-index='"+row+"']>td[data-field='result']").css('background','orangered');
                         }else if(obj.value == "BLOCK"){
                            $("tr[data-index='"+row+"']>td[data-field='result']").css('background','yellow');
                  }
                  //发送测试结果的提交
                  $.ajax({
                                    //发出请求
                                    type:"POST",
                                    url:"/basecase/case/result/manage",
                                    contentType: 'application/json; charset=utf-8',
                                    dataType:"json",
                                    data:JSON.stringify(data),
                                    success: function(result){
                                        if(result.id){
                                            layer.msg("提交成功!");
                                            resultId = result.id;
                                            $.ajax({
                                                //发出请求
                                                type:"GET",
                                                url:"/basecase/case/result/historyFail",
                                                contentType: 'application/json; charset=utf-8',
                                                dataType:"json",
                                                data:{"case_id":rowCache.id,"resultId":resultId},
                                                success: function(result){
                                                        if(result.data.length>0){
                                                        layer.confirm('查看历史FAIL记录?', function(index){

                                                                layer.open({
                                                                        type:2,
                                                                        content:"/basecase/do/history?case_id="+rowCache.id+"&resultId="+resultId,
                                                                        area:['1055px','493px'],
                                                                        title: "历史缺陷",
                                                                        closeBtn:1,
                                                                        shade: 0,
                                                                        shadeClose:true,
                                                                        maxmin: true,
                                                                        cancel: function(index, layero){

                                                                        }
                                                                 });
                                                                layer.close(index);
                                                        });
                                                    }
                                                }
                                            });

                                        }
                                    }
                  });

                  form.render('');
          });

          //点击查询

          form.on('submit(start)', function(obj){

                if(obj.field.do_plan == "" || obj.field.project_name == "" || obj.field.plan == ""){
                    layer.msg("我的任务、项目、用例版本，不能为空");
                    return false;
                }
                var loading = layer.load();

                  options = {
                    elem:'#doCase',
                    skin:"line",
                    page:true,
                    url:'/basecase/case/do?',
                    height: 'full-200',
                    toolbar: true,
                    limit:100,
                    limits:[100,500,1000],
                    where:{
                        project_version : $('#do_plan option:selected').val(),
                        project_id : $('#project_name option:selected').val(),
                        case_pri: obj.field.pri,
                        result: obj.field.result,
                        case_main_mod_list: obj.field.module,
                        case_plan: obj.field.plan,
                        key_words: $('#key_words').val(),
                    }
                  }
                  tableIns.reload(options);
                  layer.close(loading);

          });
          //监听select的选项
          form.on('select(do_plan)', function(obj){
            if(obj.value==""){return false};

            $.ajax({
                   //发出请求
                   type:"GET",
                   url:"/basecase/doCase/getTask?",
                   contentType: 'application/json; charset=utf-8',
                   dataType:"json",
                   data:{"userId":1,"project_version":obj.value},
                   success: function(result){

                      s2 = '<div class="layui-input-inline" id="taskProject">'+
                        '<label class="layui-form-label">项目*：</label>'+
                        '<div class="layui-input-inline" style="width:150px">'+
                        '    <select class="layui-select" name="project_name" id="project_name" lay-verify="" lay-search'+
                        '            lay-filter="project_name">';
                      end=' </select></div></div>';

                      if(result.data.length>0){

                           str2 = '';
                           var modelArray = new Array();
                           var casePlanArray = new Array();
                           var blSelect = false;

                           layui.each(result.data,function(k,v){
                               project_version = v.project_version;
                               project_id = v.project_id;
                               project_name = v.project_name;
                               case_plan = v.case_plan;

                               str2 = str2+'<option value="'+project_id+'">'+project_name+'</option>';
                               //模块的内容
                               caseModuleSplit = JSON.parse(v.work_content).case_module.split(',');
                               layui.each(caseModuleSplit,function(k,v){
                                    modelArray.push({"name":v,"value":v});
                               });
                               if(k==0){blSelect = true}
                               //用例版本的内容
                               casePlanArray.push({"name":JSON.parse(v.work_content).case_plan,"value":JSON.parse(v.work_content).case_plan,selected:blSelect});

                           });
                          //赋值

                          $("#taskProject").html(s2 + str2 + end);

                          var case_module = xmSelect.render({
                              el: '#case_module',
                              name:'module',
                              data: modelArray
                          });

                          var case_plan = xmSelect.render({
                              el: '#case_plan',
                              name:'plan',
                              data:casePlanArray
                          });
                          $("#case_plan_div").show();
                          $("#case_mod_div").show();

                          form.render();
                      }

                   }

               });

          });

          form.render();
    });




</script>
{% endblock %}